from __future__ import division
import os
 
def setup2(max_rate, address_data):
	import os
	#obtain list of IP addresses on the network
	ips = address_data.keys()
	length = len(ips)
	
	#clear pervious TC configurations
	clear_command_root = "tc qdisc del dev br0 root"
	os.system(clear_command_root)
	
	#create root qdisc
	setup_command_root = "tc qdisc add dev br0 root handle 1:0 htb"
	os.system(setup_command_root)
	
	#create the parent class for all information
	setup_command_mainclass = "tc class add dev br0 parent 1:1 classid 1:6 htb rate " + str(max_rate) + " ceil " + str(max_rate)
	os.system(setup_command_mainclass)
	
	#for each active IP address on the network
	for i in ips:
		#get the punishment level of the user (0 or 1 do nothing)
		punish = int(address_data[i])
		if punish == 0:
			punish = 1
		
		#create the main class for each IP
		setup_command_personalclass_root = "tc class add dev br0 parent 1:1 classid 1:6" + i + " htb rate " + str((max_rate/length)) + "bps ceil " + str((max_rate/punish)) + "bps default 6" + i + "2"
		os.system(setup_command_personalclass_root)
		
		#filter traffic to and from this IP to the class
		setup_command_personalfilter_root_src = "tc filter add dev br0 parent 1:0 protocol ip prio 5 u32 match ip src 192.168.1." + i + "/32 flowid 1:6" + i
		setup_command_personalfilter_root_dst = "tc filter add dev br0 parent 1:0 protocol ip prio 5 u32 match ip dst 192.168.1." + i + "/32 flowid 1:6" + i
		os.system(setup_command_personalfilter_root_src)
		os.system(setup_command_personalfilter_root_dst)
		
		#create subclasses for different types of traffic and filter it into the appropriate class
		setup_command_personalclass_web = "tc class add dev br0 parent 1:6" + i + " classid 1:6" + i + "1 htb rate " +  str(((max_rate/length) * 0.8)) + "bps ceil " + str((max_rate/punish)) + "bps"
		setup_command_personalclass_other = "tc class add dev br0 parent 1:6" + i + " classid 1:6" + i + "2 htb rate " + str(((max_rate/length) * 0.2)) + "bps ceil " + str((max_rate/punish)) + "bps"
		setup_command_personalfilter_web1 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip dport 80 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web2 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip sport 80 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web3 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip dport 443 0xffff flowid 1:6" + i + "1"
		setup_command_personalfilter_web4 = "tc filter add dev br0 parent 1:6" + i + " protocol ip prio 5 u32 match ip sport 443 0xffff flowid 1:6" + i + "1"
		
		os.system(setup_command_personalclass_web)
		os.system(setup_command_personalclass_other)
		os.system(setup_command_personalfilter_web1)
		os.system(setup_command_personalfilter_web2)
		os.system(setup_command_personalfilter_web3)
		os.system(setup_command_personalfilter_web4)


#f = file of data from bandwidth team
#log = log file that retains information from previous measurements to potentially
# implement QoS services that remember activity at certain times; appends f onto log each time
#pun = file that stores punishment information to keep track of abusive users
f = open('test.txt', 'r')
log = open('log.txt', 'a')
pun = open('pun.txt', 'r+')

log.write(f.read())
f.seek(0)

mx = 0
sets = {} 
punset = {}
count = 0
f.seek(0)

#read the pun file into a dictionary
for line in pun :
   (punset[line.split()[0]]) = line.split()[1]
#count the number of users online in the bandwidth file
for line in f :
   count+= 1
   mx += int(line.split()[1])

f.seek(0)

#set a punishment level of half of the measured max bandwidth
punlevel = mx/2
key = 0
value = 0

#determine if the user is over max/2, if so increment their "abusive factor", if now, decrement or leave it at 0.
for line in f :
   key = line.split()[0].split('.')[3]
   sets[key] = ""
   try:
   	value = punset[key]
   except:
   	punset[key] = 0
   	value = 0
   
   if int(line.split()[1]) > punlevel :
       punset[key] = int(value) + 1
       print punset[key] 
   else:
       if int(value) != 0 :
           punset[key] = int(value) - 1
       #print punset[key] 
#print punset    
 
#write the new punishment set to a file
wr = ""  
for key in punset :
    wr = wr + str(key) + " " + str(punset[key]) + "\n"
pun.seek(0)
pun.write(wr)

#send information to QoS function
setup2(mx*8, punset)

f.close()
log.close()
pun.close()

